package net.NingXiaRuiTai.ShortChain.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.NingXiaRuiTai.ShortChain.Manager.LinkGroupManager;
import net.NingXiaRuiTai.ShortChain.controller.request.LinkGroupAddRequest;

import net.NingXiaRuiTai.ShortChain.controller.request.LinkGroupUpdateRequest;
import net.NingXiaRuiTai.ShortChain.interceptor.LoginInterceptor;
import net.NingXiaRuiTai.ShortChain.model.LinkGroupDO;
import net.NingXiaRuiTai.ShortChain.mapper.LinkGroupMapper;
import net.NingXiaRuiTai.ShortChain.model.LoginUser;
import net.NingXiaRuiTai.ShortChain.model.VO.LinkGroupVO;
import net.NingXiaRuiTai.ShortChain.service.LinkGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yuan
 * @since 2025-02-16
 */
@Service
@Slf4j
public class LinkGroupServiceImpl extends ServiceImpl<LinkGroupMapper, LinkGroupDO> implements LinkGroupService {
    @Resource
    private LinkGroupManager linkGroupManager;
    @Override
    public int add(LinkGroupAddRequest addRequest) {
        ThreadLocal<LoginUser> threadLocal = LoginInterceptor.threadLocal;
        LoginUser loginUser = threadLocal.get();
        Long accountNo= loginUser. getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        //从请求中获取短链标题
        linkGroupDO.setTitle(addRequest.getTitle());
        //存入登录的账号编号
        linkGroupDO.setAccountNo(accountNo);
        int rows = linkGroupManager.add(linkGroupDO);


        return rows ;
    }

    @Override
    public int del(Long groupId) {
//在操作时一定要先进行鉴权(防止水平越权)
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        int rows = linkGroupManager.del(groupId, accountNo);

        return rows;
    }

    @Override
    public LinkGroupVO detail(Long id) {
        //在操作时一定要先进行鉴权(防止水平越权)
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = linkGroupManager.detail(id, accountNo);
        LinkGroupVO linkGroupVO = new LinkGroupVO();
        BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
        return linkGroupVO;

    }

    @Override
    public List<LinkGroupVO> listAllGroup() {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO>listAllGroup = linkGroupManager.listAllGroup(accountNo);

        List<LinkGroupVO> groupVOList = listAllGroup.stream().map(linkGroupDO -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(linkGroupDO, linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());


        return  groupVOList;


    }

    @Override
    public int updateById(LinkGroupUpdateRequest addRequest) {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setId(addRequest.getId());
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setTitle(addRequest.getTitle());


        int rows= linkGroupManager.updateById(linkGroupDO);
        return rows;
    }
}
